// index.js
Page({
  data: {
    wxLoginCode: "",
    user: {
      phoneNumberNewLib: "",
      phoneNumberOldLib: "",
      openId: ""
    }
  },
  getPhoneNumberFn(e) {
    console.log(e, "====");
    if (e.detail.code) {
      wx.request({
        method: 'POST',
        url: 'http://127.0.0.1:8080/v2/ma/users/phones/new',
        header: {
          'content-type': 'application/json'
        },
        data: {
          wxPhoneNumberCode: e.detail.code
        },
        success: (res) => {
          console.log(res.data, "=====");
          this.setData({
            user: {
              phoneNumberNewLib: res.data.data.phoneNumber
            }
          })
        }
      })

    }
  },
  getFuzzyLocation(e) {
    console.log(e, "====");
    wx.getFuzzyLocation({
      type: 'wgs84',
      success(res) {
        console.log(res, "=====");
        const latitude = res.latitude
        const longitude = res.longitude
        console.log("latitude:", latitude, " longitude:", longitude)
      }
    })
  },
  getPhoneNumberFnOldLibVersion(e) {
    wx.login({
      success: (res) => {
        console.log(res, "======")
        console.log(res.code, "======")
        if (e.detail.code) {
          wx.request({
            method: 'POST',
            url: 'http://127.0.0.1:8080/v1/ma/users/phones/old',
            header: {
              'content-type': 'application/json'
            },
            data: {
              wxLoginJSCode: res.code,
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv
            },
            success: (res) => {
              console.log(res.data, "=====");
              this.setData({
                user: {
                  phoneNumberNewLib: res.data.data.phoneNumber
                }
              })
            }
          })
        }

      },
    })
  },
  getMAOpenIdFn(e) {
    wx.login({
      success: (res) => {
        console.log(res, "====")
        console.log(res.code)
        this.setData({
          wxLoginCode: res.code
        })
        wx.request({
          method: 'GET',
          url: 'http://127.0.0.1:8080/v1/ma/users/openId',
          header: {
            'content-type': 'application/json'
          },
          data: {
            wxLoginJSCode: res.code
          },
          success: (res) => {
            console.log(res.data, "=====");
            this.setData({
              user: {
                openId: res.data.data.openid
              }
            })
          }
        })
      },
    })
  },
  getWxLoginCode(e) {
    wx.login({
      success: (res) => {
        console.log(res, "====")
        console.log(res.code)
        wx.setStorageSync('wxLoginCode', res.code)
        this.setData({
          wxLoginCode: res.code
        })
      },
    })
  }

})